package sky.dao.Impl;

import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import sky.dao.TicketDAO;
import sky.domain.Flight;
import sky.domain.Ticket;

import org.hibernate.Query;
import sky.domain.User;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Gauss
 * Date: 24.04.13
 * Time: 17:39
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TicketDAOImpl implements TicketDAO{

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public void addTicket(Ticket ticket) {
        sessionFactory.getCurrentSession().save(ticket);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Ticket> listTicket() {
        return sessionFactory.getCurrentSession().createQuery("from Ticket").list();
    }

    @Override
    public void removeTicket(Integer id) {
        Ticket ticket = (Ticket) sessionFactory.getCurrentSession().load(Ticket.class, id);
        if (null != ticket) {
            sessionFactory.getCurrentSession().delete(ticket);
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Ticket> getFreeTicketsByFlight(Integer id) {
        Flight flight = (Flight) sessionFactory.getCurrentSession().load(Flight.class, id);
        if (null != flight) {
            Query query = sessionFactory.getCurrentSession().createQuery("from Ticket as ticket where ticket.user.id = ? and ticket.booked = false");
            query.setParameter(0, id, Hibernate.INTEGER);
            return query.list();
        }
        return null;
    }

    @Override
    public void bookTicketByPlace(Integer id, Integer row, Character place, Integer userId) {
        Query query = sessionFactory.getCurrentSession().createQuery("from Ticket as ticket where ticket.flight.id = ? and ticket.row = ? and ticket.place = ?");
        query.setParameter(0, id, Hibernate.INTEGER);
        query.setParameter(1, row, Hibernate.INTEGER);
        query.setParameter(2, place, Hibernate.CHARACTER);
        Ticket ticket = (Ticket) query.uniqueResult();
        ticket.setBooked(true);
        ticket.setUser(new User());
        ticket.getUser().setId(userId);
    }

    @Override
    public void addTickets(Flight flight) {
        String type = "firstClass";
        for (int i = 1; i <= flight.getPlane().getRows(); i++) {
            if (i > flight.getPlane().getFirstClassPlaces()) type="bussiness";
            if (i > flight.getPlane().getFirstClassPlaces()+flight.getPlane().getBussinessPlaces()) type="econom";
            char c = 'A';
            for (int j = 1; j <= flight.getPlane().getPlaces(); j++) {
                sessionFactory.getCurrentSession().save(new Ticket(flight, type, i, c));
                c++;
            }
        }
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Integer> getFreeRows(Integer flightId) {
        Query query = sessionFactory.getCurrentSession().createQuery("select ticket.row from Ticket as ticket where ticket.flight.id = ? and ticket.booked = false group by ticket.row");
        query.setParameter(0, flightId);
        return query.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Character> getFreePlaces(Integer flightId) {
        Query query = sessionFactory.getCurrentSession().createQuery("select ticket.place from Ticket as ticket where ticket.flight.id = ? and ticket.booked = false group by ticket.place");
        query.setParameter(0, flightId);
        return query.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Character> getFreePlacesOnRow(Integer flightId, Integer row) {
        Query query = sessionFactory.getCurrentSession().createQuery("select ticket.place from Ticket as ticket where ticket.flight.id = ? and ticket.row = ? and ticket.booked = false group by ticket.place");
        query.setParameter(0, flightId);
        query.setParameter(1, row);
        return query.list();
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Integer> getFreeRowsOnPlace(Integer flightId, Character place) {
        Query query = sessionFactory.getCurrentSession().createQuery("select ticket.row from Ticket as ticket where ticket.flight.id = ? and ticket.place = ? and ticket.booked = false group by ticket.row");
        query.setParameter(0, flightId);
        query.setParameter(1, place);
        return query.list();
    }
}
